package edu.hnu.cloudflightserver.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.hnu.cloudflight.android.domain.Flight;
import edu.hnu.cloudflightserver.server.FlightServerRecord;
import edu.hnu.cloudflightserver.server.impl.FlightServerRecordImpl;
import edu.hnu.cloudflightserver.util.LogUtil;
import edu.hnu.cloudflightserver.util.ServletUtil;
import edu.hnu.cloudflightserver.util.StringUtil;

@WebServlet(name = "FlightServerRecordServlet", urlPatterns = { "/flightserverrecord" })
public class FlightServerRecordServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -816466448159064380L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		LogUtil.info("begin record");
		req.setCharacterEncoding("utf-8");
		String tasktype = req.getParameter("tasktype");
		if (StringUtil.isEmpty(tasktype)) {
			ServletUtil.responseJson(resp, "error");
			return;
		}
		FlightServerRecord server = FlightServerRecordImpl.getRecordInstance();
		Flight f = new Flight();
		f.name = req.getParameter("name");
		f.start = req.getParameter("start");
		f.end = req.getParameter("end");
		String type = req.getParameter("type");
		String userName = req.getParameter("user");
		String queryDate = req.getParameter("querydate");
		LogUtil.info(f.name,type,f.start,f.end);
		boolean flag = false;
		if("query".equals(tasktype)){
			flag = server.recordQuery(type, f, userName, queryDate);
		}else{
			flag = server.recordWarnTask(f, userName, queryDate);
		}
		if (flag) {
			ServletUtil.responseJson(resp, "success");
		} else {
			ServletUtil.responseJson(resp, "error");
		}
	}

}
